
import pandas as pd
import numpy as np

np.random.seed(0)

dates = pd.date_range("20210101", periods=6)
df = pd.DataFrame(np.random.randn(6, 5), index=dates, columns=["a", "b", "c", "d", "e"])
print(df)

# 修改某个位置的值
df.iloc[2, 3] = 1111
df.loc['20210101', 'b'] = 222
print(df)

# 根据条件修改值, 其他列的也会修改
df[df['a'] > 0] = 333
print(df)

# 根据条件修改值, 只修改某列的值
df['a'][df['a'] > 0] = 333
print(df)

# 增加空列
df['g'] = np.nan
print(df)

# 增加某列非空值， 需要指定index， 否则该列值为空
df['h'] = pd.Series([1, 2, 3, 4, 5, 6], index=df.index)
print(df)
